/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main.controller;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
import javax.faces.event.AjaxBehaviorEvent;
import main.DBmodels.User;
import main.models.RegUser;
import main.util.DAO;

/**
 *
 * @author Ariman
 */
@ManagedBean
@RequestScoped
public class RegController {

    @ManagedProperty (value = "#{regUser}")
    private RegUser regUser;
    
    private UIInput nameInput;
    private UIInput emailInput;
    private UIInput passInput;
    private UIInput pass2Input;
    private UIOutput nameError;
    private UIOutput emailError;
    private UIOutput passError;
    private UIOutput pass2Error;
    private UIOutput allError;

    public RegController() {
    }

    public String backAction() {
        return "logout";

    }

    public void keyUpListener(AjaxBehaviorEvent event) {
        String str = event.getComponent().getId();
        DAO dao = new DAO();
        if ("regName".equals(str)) {
            if (nameInput.getValue().toString().length() < 5) {
                nameError.setValue(" Sliwkom korotkiy");
            } else {
                try {
                    User user = dao.getUserByName(nameInput.getValue().toString());
                    if (user == null) {
                        nameError.setValue(" Maybe ok");
                    } else {
                        nameError.setValue(" Allready used");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(RegController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else if ("regEmail".equals(str)) {
            if (emailInput.getValue().toString().length() < 5) {
                emailError.setValue(" Nekorectniy");
            } else {
                try {
                    User user = dao.getUserByEmail(emailInput.getValue().toString());
                    if (user == null) {
                        emailError.setValue(" Maybe ok");
                    } else {
                        emailError.setValue(" Allready used");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(RegController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else if ("regPass".equals(str)) {
            if (passInput.getValue().toString().length() < 5) {
                passError.setValue(" Korotkovat, odnako");
            } else {
                passError.setValue(" Maybe ok");
            }
        } else if ("regPassSec".equals(str)) {
            if (!pass2Input.getValue().toString().equals(passInput.getValue().toString())) {
                pass2Error.setValue(" Raznie");
            } else {
                pass2Error.setValue(" Maybe ok");
            }
        }
    }

    private String Validator() {
        String str="";
        if (nameInput.getValue().toString() != null) {
            Pattern pattern = Pattern.compile("[a-z]\\w+");
            Matcher matcher = pattern.matcher(nameInput.getValue().toString().toLowerCase());
            if (!matcher.matches()) {
                str += " The name must start with a letter; ";
            }
        } else {
            str += " Name required; ";
        }
        if (emailInput.getValue().toString() != null) {
            String sDomen = "[a-z][a-z[0-9]\u005F\u002E\u002D]*[a-z||0-9]";
            String sDomen2 = "([a-z]){2,4}";            
            Pattern pattern = Pattern.compile(sDomen + "@" + sDomen + "\u002E" + sDomen2);
            Matcher matcher = pattern.matcher(emailInput.getValue().toString().toLowerCase());
            if (!matcher.matches()) {
                str += " The Email must be in correct format; ";
            }
        } else {
            str += " Email required; ";
        }
        return str;
    }

    public String registrationAction() {
        String str = Validator();
        if (str.length()==0){
            allError.setValue("Validation OK : " + str);
            return "unsuccess";
//            regUser.setRegName(nameInput.getValue().toString());
//            regUser.setRegEmail(emailInput.getValue().toString());
//            regUser.setRegPass(passInput.getValue().toString());
//            regUser.setRegPassSec(pass2Input.getValue().toString());
//            return regUser.registrationAction();
        } else {
            allError.setValue("Validation FAILED!!! :" + str);
            return "unsuccess";
        }
    }

    public UIInput getEmailInput() {
        return emailInput;
    }

    public void setEmailInput(UIInput emailInput) {
        this.emailInput = emailInput;
    }

    public UIInput getNameInput() {
        return nameInput;
    }

    public void setNameInput(UIInput nameInput) {
        this.nameInput = nameInput;
    }

    public UIInput getPass2Input() {
        return pass2Input;
    }

    public void setPass2Input(UIInput pass2Input) {
        this.pass2Input = pass2Input;
    }

    public UIInput getPassInput() {
        return passInput;
    }

    public void setPassInput(UIInput passInput) {
        this.passInput = passInput;
    }

    public RegUser getRegUser() {
        return regUser;
    }

    public void setRegUser(RegUser regUser) {
        this.regUser = regUser;
    }

    public UIOutput getNameError() {
        return nameError;
    }

    public void setNameError(UIOutput nameError) {
        this.nameError = nameError;
    }

    public UIOutput getEmailError() {
        return emailError;
    }

    public void setEmailError(UIOutput emailError) {
        this.emailError = emailError;
    }

    public UIOutput getPass2Error() {
        return pass2Error;
    }

    public void setPass2Error(UIOutput pass2Error) {
        this.pass2Error = pass2Error;
    }

    public UIOutput getPassError() {
        return passError;
    }

    public void setPassError(UIOutput passError) {
        this.passError = passError;
    }

    public UIOutput getAllError() {
        return allError;
    }

    public void setAllError(UIOutput allError) {
        this.allError = allError;
    }
}
